from datetime import datetime
import requests
from bs4 import BeautifulSoup


class Spider:
    @staticmethod
    def fetch_data(data):
        url = ""
        if data.file_type == "BULLETIN A":
            url = "https://datacenter.iers.org/availableVersions.php?id=6"
        elif data.file_type == "BULLETIN B":
            url = "https://datacenter.iers.org/availableVersions.php?id=207"
        response = requests.get(url)
        # print(response.text)
        soup = BeautifulSoup(response.text, "html.parser")
        table = soup.find("table")
        for row in table.find_all("tr"):
            columns = row.find_all("td")
            if columns:
                data.title = columns[0].get_text().strip()
                data.date = columns[1].get_text().strip()
                data.textfile = columns[3].find("a").get("href")
                data.htmlfile = columns[6].find("a").get("href")
                data.csvfile = columns[5].find("a").get("href")

                # 将日期字符串转换为日期对象
                date_obj = datetime.strptime(data.date, "%Y-%m-%d").date()
                # 检查日期是否在选定的范围内
                if data.start_date <= date_obj <= data.end_date:
                    data.tasks.append(
                        (
                            data.file_type,
                            data.title,
                            data.date,
                            data.textfile,
                            data.htmlfile,
                            data.csvfile,
                            "准备下载",
                        )
                    )
                data.tasks = list(set(data.tasks))

                # data.tasks.append((data.file_type, data.title, data.date, data.textfile,data.htmlfile,"准备下载"))
                # 在这里添加下载数据的代码
                # data.tasks[-1] = (data_type, data_detail, "下载完成")


# data = Data()
# Spider.fetch_data(data)

